package com.heyun.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.heyun.bean.HeyunChildMenu;
import com.heyun.bean.HeyunEmp;
import com.heyun.bean.HeyunMenu;
import com.heyun.service.HeyunChildMenuService;
import com.heyun.service.HeyunEmpService;
/*@RequestMapping("/user")*/
/**
 * @author 成栋梁
 *
 */
@Controller
public class EmpLoginController {
	 @Autowired
     private HeyunEmpService  hyes;
	 @Autowired
	private HeyunChildMenuService hyms;
	 
	 /**
	  * 登陆验证
	  *
	  */
	 @RequestMapping("/login")
	public String login(String loginName, String password,
			HttpServletRequest request, HttpServletResponse response) {
		HeyunEmp hy = hyes.getEmpByName(loginName);
		if (hy != null) {
			if (hy.getPassword().equals(password)) {
				if (hy.getState() == 0) {
					request.getSession().setAttribute("user", hy);
					System.out.println("**************时间" + hy.getHireDate());
					return "redirect:manage/index.jsp";
				} else {
					request.setAttribute("message", "用户状态异常，请找管理员");
					return "login";
				}
			} else {
				request.setAttribute("message", "用户名或者密码错误");

				return "login";
			}
		} else {
			request.setAttribute("message", "用户名或者密码错误");

			return "login";
		}
	}
	 /**
	  * 获取主菜单
	  *
	  */
	 @RequestMapping("/getMenu")
	 @ResponseBody
	 public Map getMenu(){
		 List<HeyunMenu>list=hyms.listMenu();
		  Map map=new HashMap();
		  map.put("menuList", list);
		  return map;
	 }
	 /**
	  * 获取子菜单
	  *
	  */
	 @RequestMapping("/getChildMenu")
	 @ResponseBody
	 public Map getChildMenu(HttpServletRequest request,String menuId){
		 Map map=new HashMap();
		HeyunEmp  hye=(HeyunEmp) request.getSession().getAttribute("user");
		int roleId= hye.getRoleId();
		System.out.println(hye.getEmpLoginName());
		Map map2=new HashMap();
		map2.put("roleId", roleId);
		map2.put("menuId", menuId);
		 List<HeyunChildMenu>list=hyms.listChildMenu(map2);
		 map.put("listChildMenu",list);
		 System.out.println(map);
		 return map;
	 }
	 /**
	  * 客户注销
	  *
	  */
	 @RequestMapping("/logout")
	 public String logout(HttpServletRequest request){
		   request.getSession().invalidate();
		 return  "redirect:login.jsp";
	 }
	 /**
	  * 获取子类指向的jspName
	  *
	  */
	 @RequestMapping("/getJspName")
	 @ResponseBody
	 public Map getJspName(String childMenuId){
		 int a =Integer.valueOf(childMenuId);
		 Map map=new HashMap();
		HeyunChildMenu hycm= hyms.getChildMenu(a);
		map.put("jspName", hycm.getJspName());
		 return map;
		 
	 }
}
